package top.lgd.service.study;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lgd.base.LgdConstants;
import top.lgd.mapper.study.ContentMapper;
import top.lgd.pojo.entity.study.Content;
import top.lgd.pojo.entity.study.Record;
import top.lgd.pojo.req.study.ContentReq;
import top.lgd.service.LgdCacheService;
import top.lgd.utils.BeanUtils;
import top.lgd.utils.DateUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 学习内容表 服务实现类
 *
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 10:10
 */
@Service
public class ContentService extends ServiceImpl<ContentMapper, Content> implements IService<Content> {

    @Autowired
    private RecordService recordService;
    @Autowired
    private LgdCacheService lgdCacheService;

    @Transactional(rollbackFor = Exception.class)
    public boolean updateProgress(ContentReq contentReq, String type, Long userId) {
        Record record = BeanUtils.copyProperties(contentReq, Record.class, "id");
        record.setContentId(contentReq.getId());
        record.setType(type);
        Integer currentIndex = contentReq.getUpdateIndex() - contentReq.getDoneIndex();
        record.setCurrentIndex(currentIndex > 0 ? currentIndex : 0);

        LocalDateTime endTime = DateUtils.toLocalDateTime(contentReq.getRemark());
        if (null == endTime) {
            endTime = LocalDateTime.now();
        }
        record.setEndTime(endTime);
        record.setStartTime(endTime.minus(contentReq.getDuration(), ChronoUnit.MINUTES));
        record.setDataUserId(userId);

        UpdateWrapper<Content> updateWrapper = new UpdateWrapper<>();
        if (contentReq.getUpdateIndex() >= contentReq.getTargetIndex()) {
            // 状态 1学习中 2完成
            record.setStatus("2");
            updateWrapper.set(LgdConstants.COLUMN_END_DATE, LocalDate.now())
                    // 状态 0-学习中 1-待学习 2-已学习
                    .set(LgdConstants.COLUMN_STATUS, "2")
                    .set(LgdConstants.COLUMN_DONE_INDEX, contentReq.getTargetIndex());
        } else {
            // 状态 1学习中 2完成
            record.setStatus("1");
            updateWrapper.set(LgdConstants.COLUMN_DONE_INDEX, contentReq.getUpdateIndex());
        }
        updateWrapper.eq(LgdConstants.COLUMN_ID, contentReq.getId())
                .eq(LgdConstants.COLUMN_DATA_USER_ID, userId);

        String today = DateUtils.today();
        lgdCacheService.delete(LgdConstants.KEY_STUDY_OVERVIEW_INFO + today + ":" + userId);
        lgdCacheService.delete(LgdConstants.KEY_STUDY_DURATION_LIST + today + ":" + userId);
        lgdCacheService.delete(LgdConstants.KEY_STUDY_CATALOG_DURATION_LIST + today + ":" + userId);
        return recordService.save(record) && update(updateWrapper);
    }
}
